package fulbitofacil.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import fulbitofacil.exception.DAOExcepcion;
import fulbitofacil.modelo.ServiciosAd;
import fulbitofacil.util.ConexionBD;

public class ServiciosAdDAO extends BaseDAO{
	
	public ServiciosAd insertar(ServiciosAd vo) throws DAOExcepcion {
		System.out.println("ServiciosDAO: insertar()");
		String query = "INSERT INTO serviciosAd (tipo,descripcion,tarifa, Canchaful_idCanchaful)" +
				" VALUES (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {

			con = ConexionBD.obtenerConexion();
			con.setAutoCommit(false);
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getTipo());
			stmt.setString(2, vo.getDescripcion());
			stmt.setDouble(3, vo.getTarifa());
			System.out.println("1");
			stmt.setInt(4, vo.getMyCanchaFul().getIdCanchaFul());			
			System.out.println("2");
			int i = stmt.executeUpdate();
			System.out.println("3");
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}			

			con.commit();
		} catch (SQLException e) {
			try {

				con.rollback();

			} catch (SQLException e1) {
				throw new DAOExcepcion(e.getMessage());
			}
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			try {
				con.setAutoCommit(true);
			} catch (SQLException e) {
				throw new DAOExcepcion(e.getMessage());
			}
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

}
